layui.use(['jquery', 'element', 'table', 'layer'],function(){
    var $ = layui.jquery;
    var element = layui.element;
    var table = layui.table;
    var layer = layui.layer;

    var microcirclerd = localStorage.getItem("microcircleId");
    var createId = localStorage.getItem('createId');
    var createDate = new Date();
    createDate = getTime(createDate);
    $("#comment").click(function () {
        var json ={
            userId:createId,
            id:microcirclerd,
            content:$("#content").val(),
            parentId:0,
            createDate:createDate
        };
        if($("#content").value==""){
            layer.msg("评论不能为空！");
        }else{
            $.ajax({
               type:"post",
               dataType:"json",
               data:json,
               url:"/comment/microcircle/addComment",
               success:function (data) {
                   if (data){
                       layer.msg("评论成功");
                       location.reload();
                   } else {
                       layer.msg("评论失败");
                   }
               }
           })
        }
    });

    select();
    function select() {
       $.ajax({
           type:"post",
           dataType:"json",
           data:{"commodityId":microcirclerd},
           url:"/comment/microcircle/selectJsComment",
           success:function (data) {
               var data1 = data.data;
               $("#table1").empty();
               if(data1.length==0){
                   $("#table1").append(
                       "<tr>"+
                           "<th class='th1'>"+
                           "<img src='/images/user-img/moren.png' id='acatarUrl' style='width:40px;height:40px;border-radius:50%;'>"+
                           "<th id='contentt'>"+"<p class='th4'>"+"还没有评论呢..."+"</p>"+"</th>"+
                       "</tr>"
                   );
               }else{
                   for (var i = 0; i < data1.length; i++) {
                       var show="";
                       if(data1[i].status=="1"){
                           show="显示中";
                       }else if(data1[i].status=="2"){
                           show="已拦截";
                       }else{
                           show="已删除";
                       }
                       $("#table1").append(
                           "<tr>"+
                           "<th class='th1'>"+
                           "<img src="+data1[i].acatarUrl+" id='acatarUrl' style='width:40px;height:40px;border-radius:50%;'>"+
                           "<th class='true_name'>"+"<p class='th2'>"+data1[i].trueName+"</p>"+"</th>"+
                           "<th class='contentt'>"+"<p class='th3'>"+data1[i].content+"</p>"+"</th>"+
                           "<th class='create_date'>"+"<p class='th4'>"+data1[i].createDate+"</p>"+"</th>"+
                           "<th class='status'>"+"<span style='color: #F5B950'>"+show+"</span>"+"</th>"+
                           "</th>"+
                           "</tr>"
                       );
                   };
                   updClass();
               }
           }
       });
    };

    function updClass() {
        var status = $(".status").children("span");
        for (var j = 0; j < status.length; j++) {
            if(status[j].innerText=="已拦截"){
                status[j].style.color="#009688";
            }else if (status[j].innerText=="已删除") {
                status[j].style.color="#037EFEgit push -u origin master -f";
            }else{
                status[j].style.color="#F5B950";
            }
        }
        //console.log(table);
    }

    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
});